<template>
	<view class="u-padding-left-32 u-padding-right-32" :class="currentTheme">
		<a-nabbar name="提现" />
		<view class="pay" id="money">
			<view class="u-font-30 box-title text-bold"> 提现到{{name}}</view>
			<view class="u-flex u-row-between u-margin-top-35" v-if="payList.length" @click="show = true">
				<view>
					<view class="u-font-34">
						<text class="content-desc-item">姓名：</text><text class="content-desc-item">{{form.realName}}</text>
					</view>
					<view class="u-margin-top-30 u-font-34">
						<text class="content-desc-item">账号：</text><text class="text-color u-font-32">{{form.loginId}}</text>
					</view>
				</view>
				<u-icon name="gengduopsd" custom-prefix="custom-icon" :color="currentThemeObj.primary" size="30"></u-icon>
			</view>
			<view v-else class="u-flex u-row-between u-margin-top-65 u-font-36 addPay" @click="addPay">
				<view class="text-bold text-color-txtTheme">添加{{name}}账号</view>
				<u-icon name="gengduopsd" custom-prefix="custom-icon" :color="currentThemeObj.primary" size="30"></u-icon>
			</view>
			<view class="u-margin-top-62">
				<item-box>
					<view class="u-padding-top-10 u-padding-bottom-10 u-padding-left-24 u-padding-right-24">
						<view class="u-border-bottom">
							<text class="u-font-32 box-title text-bold">提现金额</text>
							<view class="u-flex money-input-view">
								<view>
									<u-icon name="icon-qian" custom-prefix="custom-icon" :color="currentThemeObj.primary" size="56"></u-icon>
								</view>
								<u-input type="digit" v-model="form.price" :clearable="false" placeholder="请输入提现金额"
									placeholder-style="color:#fffff;font-size:34rpx" :custom-style="{fontSize:'100rpx',height:'110rpx'}"/>
							</view>
						</view>
						<view class="text-tips u-padding-bottom-12">
							当前账户余额<text class="text-color-txtTheme"> {{balance}} </text>元
							<template v-if="balance>0">
								，<text @click="allMoney" class="text-color-txtTheme">全部提现</text>
							</template>
						</view>
					</view>
				</item-box>
			</view>
		</view>
		<view class="btn"><item-btn  @click="submitPay" width="100%" height="88">确定</item-btn></view>
		<all-pay :visible.sync="show" :payType="type" :currentIndex="activePaylistItemIndex" @close="close" :payList="payList" @payUser="getPayUser"/>
		<payMoneyModal :showPwdModal="showPwdModal" :form="form" @changeshowPwdModal="changeshowPwdModal"></payMoneyModal>
	</view>
</template>

<script>
	import allPay from "./wallet/allPay";
	import payMixins from "@/mixins/payMixins";
	import { BigNumber } from "bignumber.js";
	import payMoneyModal from './payMoneyModal'
	export default {
		name: "payMoney",
		components: {
			allPay,payMoneyModal
		},
		mixins: [payMixins],
		data() {
			return {
				type: '',
				show: false,
				option: {},
				form: {
					price: '',
					realName:'',
					loginId:'',
					serviceCharge:'0.1',
				},
				payList: [],
				showPwdModal:false,
				activePaylistItemIndex:0 //当前选中的index
			}
		},
		computed: {
			name() {
				if (this.type == 1) return '支付宝'
				if (this.type == 2) return '微信'
				if (this.type == 3) return '银行卡'
			}
		},
		async onShow() {
			this.getBalance() // 获取余额
			this.activePaylistItemIndex = uni.getStorageSync('activePaylistItemIndex') || 0 // 获取保存的数据 然后设置
			await this.payUserList() // 获取当前选择的账户列表
			if(this.payList.length>0){
				const item = this.payList[this.activePaylistItemIndex]
				this.form.loginId = item.loginId
				this.form.realName = item.realName
				this.form.userAccountId = item.id
			}
		},
		onHide() {
			//隐藏页面的时候保存选中的数据
			uni.setStorageSync('activePaylistItemIndex',this.activePaylistItemIndex)
		},
		onLoad() {
			//每次首次进入清理上次设置的数据 
			uni.removeStorageSync('activePaylistItemIndex')
		},
		methods: {
			// 全部提现
			allMoney() {
				this.form.price = this.balance
			},
			// 添加账户
			addPay() {
				if (this.type != 3) {
					this.$u.route({
						url: '/pagesB/wallet/addPay',
						params: {
							name: this.name,
							type:this.type
						}
					})
				} else {
					this.$u.route({
						url: '/pagesB/wallet/addCard',
					})
				}
			},
			// 确认支付
			submitPay() {
				if (!this.form.realName) {
					this.$u.toast(`请添加${this.name}账户`)
					return false
				}
				if (!this.form.price || BigNumber(this.form.price).toNumber()<1) {
					this.$u.toast('提现金额不能为空或者小于1元')
					return false
				}

				const x = new BigNumber(this.form.price);
				const y = new BigNumber(this.balance);
				if(x.comparedTo(y)==1){
					this.$u.toast('你没这么多余额吧?')
					return false;
				}
				const curMoney = (Math.ceil(((x*0.006).toFixed(3)* 100)) /100);
				console.log((x*0.006).toFixed(3))
				console.log(curMoney)
				if(curMoney<0.1){
					console.log("不足0.1，按0.1计算")
					this.form.price = x.toNumber();
					this.form.serviceCharge=0.1
				}else{
					this.form.price = x.toNumber();
					this.form.serviceCharge=curMoney
				}

				if(this.isPaymentPsd==0){
					uni.showModal({
						title:'温馨提示',
						content:'首次提现需设置支付密码',
						confirmText:'去设置',
						success: ({confirm}) => {
							if(confirm){
								this.toPassword({
									type:1,
									...this.form
								})
							}
						}
					})
					return false;
				}
				//输入密码支付
				this.changeshowPwdModal();
				// this.toPassword({
				// 	type:3,
				// 	...this.form
				// })
			},
			changeshowPwdModal(){
				this.showPwdModal = !this.showPwdModal;
			},
			toPassword(params){
				this.$u.route({
					url: '/pagesB/wallet/payPassword',
					params
				})
			},
			close() {
				this.show = false
			},
			//获取选择的账户
			getPayUser(item){
				this.form.loginId = item.loginId
				this.form.realName = item.realName
				this.form.userAccountId = item.id
				const index = this.payList.findIndex(val=>val.id===item.id);
				if(index>-1) this.activePaylistItemIndex = index
			}
		}

	}
</script>
<style lang="scss">
	#money .u-input {
		margin-left: 55rpx;
	}
</style>
<style scoped lang="scss">
	.pay {
		margin-top: 55rpx;
		.money-input-view{
			margin-top: 90rpx;
			margin-bottom: 27rpx;
			border-bottom: 1rpx solid transparentize($color: #fff, $amount: 0.9);
		}
		.text-color{
			color: var(--theme-color);
		}
	}
	.btn{
		margin-top: 311rpx;
	}
</style>
